package 纯字符串;

public class NC1大数加法 {

    /**
     * 以字符串的形式读入两个数字，编写一个函数计算它们的和，以字符串形式返回。
     * （字符串长度不大于100000，保证字符串仅由'0'~'9'这10种字符组成）
     * 示例1
     * 输入
     * 复制
     * "1","99"
     * 返回值
     * 复制
     * "100"
     * 说明
     * 1+99=100
     */

    public String solve (String s, String t) {

        if(s==null||s.length()==0){
            s="0";
        }
        if(t==null||t.length()==0){
            t="0";
        }

        int num1=0;
        int num2=0;
        int sum=0;
        int carry=0;


        char[] arr1 = new StringBuilder(s).reverse().toString().toCharArray();
        char[] arr2 = new StringBuilder(t).reverse().toString().toCharArray();

        int length=Math.min(arr1.length,arr2.length);
        StringBuilder sb=new StringBuilder();

        for (int i = 0; i < length; i++) {

            num1=arr1[i]-'0';
            num2=arr2[i]-'0';
            sum=num1+num2+carry;
            carry=sum/10;
            sb.append(sum%10);

        }

        if(arr1.length!=arr2.length){
            if(arr1.length==length){
                //继续遍历arr2
                for (int i = arr1.length; i < arr2.length; i++) {
                    num2=arr2[i]-'0';
                    sum=num2+carry;
                    carry=sum/10;
                    sb.append(sum%10);
                }
            }else{
                //继续遍历arr1
                for (int i = arr2.length; i < arr1.length; i++) {
                    num1=arr1[i]-'0';
                    sum=num1+carry;
                    carry=sum/10;
                    sb.append(sum%10);
                }
            }
        }

        if(carry!=0) {
            sb.append(carry);
        }

        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        NC1大数加法 n=new NC1大数加法();
        String result = n.solve("98", "9");
        System.out.println(result);
    }

}
